package com.resume.aipeople.controller;

import com.resume.aipeople.service.ai.LLMService;
import com.resume.aipeople.service.ai.model.LLMModelManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 模型管理控制器
 */
@RestController
@RequestMapping("/api/model")
@RequiredArgsConstructor
@Slf4j
@CrossOrigin(origins = "*")
public class ModelController {
    
    private final LLMService llmService;
    
    /**
     * 获取当前模型信息
     */
    @GetMapping("/current")
    public ResponseEntity<Map<String, Object>> getCurrentModel() {
        Map<String, Object> response = new HashMap<>();
        response.put("modelName", llmService.getCurrentModelDisplayName());
        response.put("success", true);
        return ResponseEntity.ok(response);
    }
    
    /**
     * 获取所有可用模型
     */
    @GetMapping("/available")
    public ResponseEntity<Map<String, Object>> getAvailableModels() {
        Map<String, Object> response = new HashMap<>();
        response.put("models", llmService.getAvailableModels());
        response.put("success", true);
        return ResponseEntity.ok(response);
    }
    
    /**
     * 切换模型
     */
    @PostMapping("/switch")
    public ResponseEntity<Map<String, Object>> switchModel(@RequestBody Map<String, String> request) {
        String modelName = request.get("modelName");
        Map<String, Object> response = new HashMap<>();
        
        if (modelName == null || modelName.trim().isEmpty()) {
            response.put("success", false);
            response.put("message", "模型名称不能为空");
            return ResponseEntity.badRequest().body(response);
        }
        
        try {
            boolean success = llmService.switchModel(modelName);
            if (success) {
                response.put("success", true);
                response.put("message", "模型切换成功");
                response.put("currentModel", llmService.getCurrentModelDisplayName());
                log.info("模型切换成功: {}", modelName);
            } else {
                response.put("success", false);
                response.put("message", "模型切换失败，请检查模型是否可用");
            }
        } catch (Exception e) {
            log.error("模型切换异常: {}", e.getMessage(), e);
            response.put("success", false);
            response.put("message", "模型切换异常: " + e.getMessage());
        }
        
        return ResponseEntity.ok(response);
    }
}
